package cn.tansanqinger.service.hy_menu.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.hy_menu.HyMenuSqlDaoImpl;
import cn.tansanqinger.pojo.HyMenu;
import cn.tansanqinger.service.hy_menu.IHyMenuService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class HyMenuServiceImpl implements IHyMenuService {
	
	@Autowired
	private HyMenuSqlDaoImpl daoImpl;
	
	@Override
	public List<HyMenu> selectFirstMenus() {
		return daoImpl.selectFirstMenus();
	}

	@Override
	public List<HyMenu> selectHyMenus(PageData pd) {
		int page = Integer.parseInt(Tools.isEmpty(pd.get("page")));
		int limit = Integer.parseInt(Tools.isEmpty(pd.get("limit")));
		int hparentId = -1;
		if(Tools.isEmpty(pd.get("hparentId")).matches("\\d+")) {
			hparentId = Integer.parseInt(Tools.isEmpty(pd.get("hparentId")));
		}
		String keywords = Tools.isEmpty(pd.get("keywords"));
		return daoImpl.selectHy_Menus(page, limit,keywords,hparentId);
	}

	@Override
	public int selectHyMenuLength(PageData pd) {
		int page = Integer.parseInt(Tools.isEmpty(pd.get("page")));
		int limit = Integer.parseInt(Tools.isEmpty(pd.get("limit")));
		int hparentId = -1;
		if(Tools.isEmpty(pd.get("hparentId")).matches("\\d+")) {
			hparentId = Integer.parseInt(Tools.isEmpty(pd.get("hparentId")));
		}
		String keywords = Tools.isEmpty(pd.get("keywords"));
		return daoImpl.selectHy_MenusLength(page,limit,keywords,hparentId);
	}

	@Override
	public int selectSnMax() {
		return daoImpl.selectSnMax();
	}

	@Override
	public int serialNumber(PageData pageData) {
		if("菜单".equals(pageData.get("hmenu_name"))) {
			return 1;
		}
		HyMenu hyMenu = daoImpl.selectHmenuId(pageData.get("hmenu_name"));
		if(hyMenu!=null) {
			hyMenu = daoImpl.serialNumber(hyMenu.getHmenuId());
			if(hyMenu==null) {
				return 2;
			} else {
				return hyMenu.getHmenuOrder()+1;
			}
		} else {
			return 0;
		}
	}
	
	@Override
	public PageData addMenu(PageData pageData) {
		/**
		 * 首先需要验证分类和序号是否可以对应，然后对菜单是否可以上线进行验证
		 */
		try {
			pageData.put("hmenu_name",pageData.get("menuClassify"));
			int menuOrder = serialNumber(pageData);
			if(pageData.get("menuOrder").toString().matches("\\d+")) {
				if(Integer.parseInt(pageData.get("menuOrder").toString()) == menuOrder) {
					HyMenu hyMenu = new HyMenu();
					hyMenu.setHmenuName(pageData.get("menuName").toString());
					HyMenu menu = daoImpl.selectHmenuId(pageData.get("menuClassify"));
					if(menu==null) {
						hyMenu.setHparentId(0);
					} else {
						hyMenu.setHparentId(menu.getHmenuId());
					}
					hyMenu.setHmenuTime(DateUtil.getTime());
					hyMenu.setHmenuCtime(DateUtil.getTime());
					hyMenu.setHmenuUrl(pageData.get("menuUrl").toString());
					hyMenu.setHmenuOrder(menuOrder);
					if("是".equals(pageData.get("isPopUpOnline"))) {
						hyMenu.setIsOnSale(1);
					} else if("否".equals(pageData.get("isPopUpOnline"))) {
						hyMenu.setIsOnSale(0);
					} else {
						pageData.put("code", "-1");
						pageData.put("msg", "菜单是否上线数据出错");
						return pageData;
					}
					hyMenu.setHmenuSn(selectSnMax()+1);
					int count = daoImpl.insertMenu(hyMenu);
					if(1==count) {
						pageData.put("code", "0");
						pageData.put("msg", "数据添加成功");
					} else {
						pageData.put("code", "-1");
						pageData.put("msg", "数据添加失败");
					}
				}
			}
		}catch (Exception e) {
			pageData.put("code", "-1");
			pageData.put("msg", "数据出错");
			return pageData;
		}
		return pageData;
	}

	@Override
	public HyMenu selectMenuIdOne(PageData pageData) {
		String menuId = pageData.get("hmenuId").toString();
		if(menuId.matches("\\d+")) {
			return daoImpl.selectMenuIdOne(Integer.parseInt(menuId));
		}
		return null;
	}

	@Override
	public PageData updataMenu(PageData pageData) {
		try {
			HyMenu hyMenu = daoImpl.selectMenuIdOne(Integer.parseInt(pageData.get("menuId").toString()));
			int count = 0;
			if (!hyMenu.getHmenuName().equals(pageData.get("menuName"))) {
				pageData.put("hmenu_name",pageData.get("menuClassify"));
				count = serialNumber(pageData);
			} else {
				count = hyMenu.getHmenuOrder();
			}
			if(pageData.get("menuOrder").toString().matches("\\d+")) {
				if(Integer.parseInt(pageData.get("menuOrder").toString())==count) {
					HyMenu menu = daoImpl.selectHmenuId(pageData.get("menuClassify"));
					if(menu==null) {
						hyMenu.setHparentId(0);
					} else {
						hyMenu.setHparentId(menu.getHmenuId());
					}
					hyMenu.setHmenuName(Tools.isEmpty(pageData.get("menuName")));
					hyMenu.setHmenuUrl(Tools.isEmpty(pageData.get("menuUrl")));
					hyMenu.setHmenuOrder(count);
					hyMenu.setHmenuCtime(DateUtil.getTime());
					if("是".equals(pageData.get("isPopUpOnline"))) {
						hyMenu.setIsOnSale(1);
					} else if("否".equals(pageData.get("isPopUpOnline"))) {
						hyMenu.setIsOnSale(0);
					}
					if (daoImpl.updataMenu(hyMenu)==1) {
						pageData.put("code", "0");
						pageData.put("msg", "数据修改成功");
						return pageData;
					} else {
						pageData.put("code", "-1");
						pageData.put("msg", "数据修改失败");
						return pageData;
					}
				} 
			} 
			pageData.put("code", "-1");
			pageData.put("msg", "数据填写有误");
		} catch (Exception e) {
			pageData.put("code", "-1");
			pageData.put("msg", "数据出错");
			return pageData;
		}
		return pageData;
	}

	@Override
	public PageData updateMenuStatus(PageData pageData) {
		try {
			int hmenuId = Integer.parseInt(Tools.isEmpty(pageData.get("hmenuId")));
			int isOnSale = Integer.parseInt(Tools.isEmpty(pageData.get("isOnSale")));
			int count = daoImpl.updateMenuStatus(hmenuId,isOnSale);
			if(count == 1) {
				pageData.put("code", "0");
				if(isOnSale == 1) {
					pageData.put("msg", "数据成功上线");
				} else {
					pageData.put("msg", "数据成功下线");
				}
				return pageData;
			} else {
				pageData.put("code", "-1");
				if(isOnSale == 1) {
					pageData.put("msg", "数据上线失败");
				} else {
					pageData.put("msg", "数据下线失败");
				}
				return pageData;
			}
		} catch (Exception e) {
			pageData.put("code", "-1");
			pageData.put("msg", "数据异常");
			return pageData;
		}
	}

	@Override
	public PageData deleteMenu(PageData pageData) {
		try {
			if(Tools.isEmpty(pageData.get("hmenuId")).matches("\\d+")) {
				int hmenuId = Integer.parseInt(Tools.isEmpty(pageData.get("hmenuId")));
				if(1==daoImpl.deleteMenu(hmenuId)) {
					pageData.put("code", "0");
					pageData.put("msg", "数据删除成功");
				} else {
					pageData.put("code", "-1");
					pageData.put("msg", "数据删除失败");
				}
			}
		} catch (Exception e) {
			pageData.put("code", "-1");
			pageData.put("msg", "数据异常");
			return pageData;
		}
		return pageData;
	}

}
